package com.wangtianxiang.rabbitmq.receive;

import com.rabbitmq.client.Channel;
import com.wangtianxiang.rabbitmq.domain.Order;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.*;
import org.springframework.amqp.support.AmqpHeaders;
import org.springframework.messaging.handler.annotation.Header;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.stereotype.Component;

import java.io.IOException;


//@Component
//@RabbitListener(bindings = @QueueBinding(value = @Queue(value = "queue_email",durable = "true"),
//        exchange = @Exchange(value = "topic.exchange",ignoreDeclarationExceptions = "true",type = ExchangeTypes.TOPIC),
//        key = {"topic.#.email.#","email.*"}))
public class EmailTopicReceiver {

    @RabbitHandler
    public void processMessage(Order message){
        System.out.println("Order message");
    }

    @RabbitHandler
    public void processMessage(@Header(AmqpHeaders.DELIVERY_TAG) long deliveryTag, String message, Channel channel)
    throws IOException{
        System.out.println("接收的mq消息："+message);
        System.out.println("deliveryTag:" + deliveryTag);
        try {
//            System.out.println(1/0);
            //成功确认

            channel.basicAck(deliveryTag,true);
        }catch (Exception e){
            //第二个参数为true时，让消息重回队列进行消费，容易出现死循环。第二个参数为false，则放弃消息。
            channel.basicNack(deliveryTag,false,true);
            e.printStackTrace();

        }



    }
}
